---@module hwMqttCmdHandle
-- 华为云mqtt信息出来
require "log"
require "tool"
require "deviceConfig"
require "mqttOutMsg"
require "uart485"

module(..., package.seeall)

-- API路由,华为云里面定义的命令
-- 注意一次返回的内容不要太多否则mqtt报错，实测不要超1000字节
local apiHandle = {
    ["CMD_MOTORGO"] = function(para)
    end,

    -- 远程重启
    ["CMD_REBOOT"] = function(para)
        local re = { ["code"] = 0, ["msg"] = 'scuess will reboot after 5 sec' }
        sys.taskInit(function()
            log.info("CMD_REBOOT,", "scuess will reboot after 5 sec")
            sys.wait(5000)
            sys.restart("CMD_REBOOT")
        end)
        return re
    end,

    -- 马上上报一次当前料量信息
    ["REPORTNOW"] = function(para)
        local re = { ["code"] = 0, ["msg"] = 'scuess reportnow' }
        sys.taskInit(function()
            log.info("REPORTNOW")
            sys.wait(1000)
            mqttOutMsg.wantSendServiceAll()
        end)
        return re
    end,

    -- 设置连接的仪表类型,设置后进行一次重启
    ["SETDEVTYPE"] = function(para)
        local typeset = para["typeset"]
        log.info("cmd SETDEVTYPE ", "typeset==", typeset)
        if typeset ~= nil and
                (typeset == "NM" or typeset == "TLD" or typeset == "QH" or typeset == "TLD2" or typeset ==
                        "BH" or typeset == "FGSG" or typeset == "SHYH" or typeset ==
                        "SHYHA9" or typeset == "TCPW" or typeset == "SHYHC8" or typeset ==
                        "DNQ" or typeset == "YFKS" or typeset == "XK31682" or typeset == "SHYHC602" or typeset ==
                        "XK3168") then
            nvm.set("DEV_TYPE", typeset)
            local re = { ["code"] = 0, ["msg"] = 'success will restart' }
            sys.taskInit(function()
                log.info("SETDEVTYPE_REBOOT,", "scuess will reboot after 5 sec")
                sys.wait(5000)
                sys.restart("SETDEVTYPE_REBOOT")
            end)
            return re
        else
            local re1 = {
                ["code"] = 3,
                ["msg"] = 'error typeset must in NM,TLD,BH,FGSG,4800ZF,9600ZF'
            }

            return re1

        end
    end,

    -- 设置串口地址位
    ["CMD_ADDR"] = function(para)
        local typeset = para["data"]
        log.info("cmd CMD_ADDR ", "CMD_ADDR==", typeset)
        if typeset ~= nil then
            nvm.set("SLAVE_ID", typeset)
            local re = { ["code"] = 0, ["msg"] = 'success will restart' }
            sys.taskInit(function()
                log.info("CMD_ADDR,", "scuess will reboot after 5 sec")
                sys.wait(5000)
                sys.restart("CMD_ADDR")
            end)
            return re
        else
            local re1 = { ["code"] = 3, ["msg"] = 'error' }
            return re1
        end
    end,

    -- 设置串口波特率
    ["CMD_BAUD"] = function(para)
        local typeset = para["data"]
        log.info("cmd CMD_BAUD ", "CMD_BAUD==", typeset)
        if typeset ~= nil then
            nvm.set("BAUD", tonumber(typeset))
            local re = { ["code"] = 0, ["msg"] = 'success will restart' }
            sys.taskInit(function()
                log.info("CMD_BAUD,", "scuess will reboot after 5 sec")
                sys.wait(5000)
                sys.restart("CMD_BAUD")
            end)
            return re
        else
            local re1 = { ["code"] = 3, ["msg"] = 'error' }
            return re1
        end
    end,

    ["CMD_HEX"] = function(para)
        local data = para["hexData"]
        local result, cmd_buf = "", ""
        local re = ""
        sys.taskInit(function()
            uart485.modbusSend(string.fromHex(data))
            sys.wait(3000)
            result, cmd_buf = sys.waitUntil("MY_UART_REC_DAT", 3000)
            if result then
                re = { ["code"] = 0, ["msg"] = cmd_buf }
            else
                re = { ["code"] = 3, ["msg"] = "fail" }
            end
        end)
        return re
    end

}

function handleCmd(command, para)
    if apiHandle[command] ~= nil then
        log.info("handleCmd", command)
        return apiHandle[command](para)
    else
        log.error("hwMqttCmdHandle", "cmd not definde")
    end

end

